﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CloudFranceTriple.Models;
using CloudFranceTriple.WebRole.Models;

namespace CloudFranceTriple.WebRole.Areas.Member.Controllers
{
    [CloudFranceTriple.WebRole.Filters.InitializeSimpleMembership]
    public class AccountingEntryController : Controller
    {
        private cloudfrAboq2RsDMEntities db = new cloudfrAboq2RsDMEntities();

        //
        // GET: /Member/AccountingEntry/

        [ChildActionOnly]
        [Authorize(Roles = "RoleReaderAccount,RoleAdminAccount")]
        public ActionResult ShowSold()
        {
            var _today = DateTime.UtcNow.Date.AddDays(1);
            var query = (from entry in db.AccountingEntrySet
                         group entry by entry.AccountSet into account
                         select new TeamAccountSold
                         {
                             Label = account.Key.Label,
                             SoldPurchase = account.Sum(ac => ac.CreditAmount ?? 0) - account.Sum(ac1 => ac1.DebitAmount ?? 0),
                             SoldBank = account.Where(ac2 => ac2.BankDate != null).Where(ac3 => ac3.BankDate <= _today).Sum(ac4 => ac4.CreditAmount ?? 0) - account.Where(ac2 => ac2.BankDate != null).Where(ac3 => ac3.BankDate <= _today).Sum(ac5 => ac5.DebitAmount ?? 0)
                         }).OrderBy(result => result.Label).ToList();
            return PartialView("ShowSold", query);
        }

        //
        // GET: /Member/AccountingEntry/

        [Authorize(Roles = "RoleReaderAccount,RoleAdminAccount")]
        public ActionResult Index()
        {
            return View(db.AccountingEntrySet.Include("AccountSet").OrderByDescending(account => account.PurchaseDate).ToList());
        }

        //
        // GET: /Member/AccountingEntry/Create

        [Authorize(Roles = "RoleAdminAccount")]
        public ActionResult Create()
        {
            ViewBag.AccountSet_Id = new SelectList(db.AccountSet.OrderBy(account=>account.Label), "Id", "Label");
            return View();
        }

        //
        // POST: /Member/AccountingEntry/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "RoleAdminAccount")]
        public ActionResult Create(AccountingEntrySet accountingentryset)
        {
            if (ModelState.IsValid)
            {
                accountingentryset.Author = User.Identity.Name;
                accountingentryset.AuthorIp = Request.UserHostAddress.Substring(0, Request.UserHostAddress.Length > 15 ? 15 : Request.UserHostAddress.Length);
                db.AccountingEntrySet.Add(accountingentryset);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(accountingentryset);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}